package vn.fsoft.gst.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import vn.fsoft.gst.domain.Department;

/**
 * implement services of department
 * @author Tung
 */

@Repository
public class DepartmentDAOImp implements DepartmentDAO {
	
	@Autowired
	private SessionFactory sessionFactory;
    
    public Integer addDepartment(Department object) {
    	return (Integer)sessionFactory.getCurrentSession().save(object);
    }
    
    @SuppressWarnings("unchecked")
	public List<Department> getListDepartment() {
    	List<Department> listDepartment = null;
    	
    	listDepartment = sessionFactory.getCurrentSession().createQuery("from Department").list();
    	
    	return listDepartment;
    }
    

    public int editDepartment(Department object) {
    	int rowCount = 0;
        sessionFactory.getCurrentSession().saveOrUpdate("Department", object);        
       
    	return rowCount;
    }
    
    public Department getDepartmentById(int id) {
    	Department department = null;
    	
    	String hsql = "from Department where departmentID = :departmentID";
    	Query query = sessionFactory.getCurrentSession().createQuery(hsql);
    	query.setInteger("departmentID", id);
    	department = (Department)query.list().get(0);
    	
    	return department;
    }
    
    public int markActiveById(int id) {
    	int rowEffect = 0;
    	
    	String hsql = "update Department set isActive = :isActive "
    			+ "where departmentID = :departmentID";
    	Query query = sessionFactory.getCurrentSession().createQuery(hsql);
    	query.setBoolean("isActive", true);
    	query.setInteger("departmentID", id);
    	rowEffect = query.executeUpdate();
    	
    	return rowEffect;
    }
    
    @SuppressWarnings("unchecked")
    public List<Department> getListDepartmentByDirecId(int id) {
    	List<Department> listDepartment;
    	
    	String hsql = "from Department where directorate.directorateID = :id";
    	Query query = sessionFactory.getCurrentSession().createQuery(hsql);
    	query.setInteger("id", id);
    	listDepartment = query.list();
    	
    	return listDepartment;
    }
    
    public int markInActiveById(int id) {
    	int rowEffect = 0;
    	
    	String hsql = "update Department set isActive = :isActive "
    			+ "where departmentID = :departmentID";
    	Query query = sessionFactory.getCurrentSession().createQuery(hsql);
    	query.setBoolean("isActive", false);
    	query.setInteger("departmentID", id);
    	rowEffect = query.executeUpdate();
    	
    	return rowEffect;
    }
    
    public boolean checkUniqueName(String name) {
    	boolean flag = false;
    
    	String hsql = "from Department "
    			+ "where departmentName = :departmentName";
    	Query query = sessionFactory.getCurrentSession().createQuery(hsql);
    	query.setString("departmentName", name);
    	flag = (query.list().size() == 0) ? true : false;
    	
    	return flag;
    }
    
}
